package com.itany.shop.controller;

import com.itany.shop.entity.Order;
import com.itany.shop.entity.User;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.OrderService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author MissChen
 * @date 2024年05月17日 9:58
 */
@WebServlet("*.order")
public class OrderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path=request.getServletPath();
        switch (path){
            case "/create.order":
                createOrder(request,response);
                break;
            case "/findMyOrders.order":
                findMyOrders(request,response);
                break;
            case "/findDetail.order":
                findDetail(request,response);
                break;
            default:
                PrintWriter out=response.getWriter();
                out.println("<h1 style='color:red;'>您输入的要求有误，请重新输入</h1>");
        }

    }
    public void createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService= (OrderService) ObjectFactory.getObject("orderService");
        //从session中取出购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        //从session中取出user
        User user= (User) request.getSession().getAttribute("user");
        try {
           String no=orderService.createOrder(cart,user);

           //因为后面用的是重定向，订单号保存到session作用域
           request.getSession().setAttribute("no",no);

           //订单已经生成，购物车中的数据已经无效，因此需要清除原有的购物车数据
           request.getSession().setAttribute("cart",new Cart());

           //生成订单后重定向到对应页面
            response.sendRedirect(request.getContextPath()+"/jsp/success.jsp");



        } catch (UserNotLoginException e) {
            e.printStackTrace();
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("orderMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/confirm.jsp").forward(request,response);
        }
    }

    public void findMyOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService= (OrderService) ObjectFactory.getObject("orderService");
        //从session中取出购物车
        User user= (User) request.getSession().getAttribute("user");
        try {
            List<Order> orders=orderService.findMyOrders(user);
            request.setAttribute("orders",orders);
            request.getRequestDispatcher("/jsp/myOrders.jsp").forward(request,response);
        } catch (UserNotExistException e) {
            e.printStackTrace();
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {

            e.printStackTrace();
            request.setAttribute("productMsg",e.getMessage());
            request.getRequestDispatcher("/findAll.product").forward(request,response);
        }
    }

    public void findDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService= (OrderService) ObjectFactory.getObject("orderService");
        String id=request.getParameter("id");
        try {

            Order order=orderService.findOrderDetail(Integer.parseInt(id));
            request.setAttribute("order",order);
            request.getRequestDispatcher("/jsp/orderDetail.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            request.setAttribute("orderMsg",e.getMessage());
            request.getRequestDispatcher("/findMyOrders.order").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("orderMsg","服务器内部异常");
            request.getRequestDispatcher("/findMyOrders.order").forward(request,response);
        }
    }
}
